package tech.yunland.shop.domain.service;

import com.moyuer.bsf.domain.dao.*;
import com.moyuer.bsf.domain.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tech.yunland.shop.domain.model.SchedulerJob;

import java.util.Calendar;

/**
 * 注册服务
 *
 * @author: 袁首京<yuanshoujing@gmail.com>
 * 16/5/7
 */
@Service
public class RegisterService {

    /**
     * 模板店铺角色标识
     */
    private static final String TEMPLATE_SHOP_ROLE_ID = "5d152778-1b6e-449b-9fee-3b8739cac89d";
    /**
     * 模板会员角色标识
     */
    private static final String TEMPLATE_MEMBER_ROLE_ID = "69772547-bfa9-427c-b639-992f0b4ff2e9";

    @Autowired
    private OrganDao organDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserOrgansDao userOrgansDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private UserOpenIdDao userOpenIdDao;
    @Autowired
    private IMainCategoryInit iMainCategoryInit;

    @Autowired
    private SchedulerJobService schedulerJobService;

    @Value("${probation_days}")
    private String probationDays;

    /**
     * 用户注册
     *
     * @param user
     */
    public void addForRegister(User user) {
        // 保存用户
        userDao.save(user);

        // 为其创建主机构
        Organ organ = setPrimaryOrgan(user);
        if ("10".equals(user.getKindCode())) {//只有注册机构用户才可以初始化栏目组根结点
            iMainCategoryInit.initCategory(organ,user);

            //设置试用期
            SchedulerJob schedulerJob = new SchedulerJob();
            try {
                Calendar instance = Calendar.getInstance();
                instance.add(Calendar.DATE,Integer.valueOf(probationDays).intValue());
                schedulerJob.setEndTime(instance.getTime());
                schedulerJob.setOrgan(organ);
                schedulerJob.setCreator(user);
                schedulerJobService.createSchedulerJob(schedulerJob);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 为其分配角色
        setRole(user);
    }

    /**
     * 第三方账号注册
     *
     * @param userOpenId
     */
    public void addForRegister(UserOpenId userOpenId) {
        addForRegister(userOpenId.getOwner());
        userOpenIdDao.save(userOpenId);
    }

    /**
     * 为新注册的用户创建机构
     *
     * @param user
     */
    private Organ setPrimaryOrgan(User user) {
        Organ root = organDao.get(Organ.ROOT_ID);

        Organ organ = null;
        if ("10".equals(user.getKindCode())) {
            organ = new Organ();
            organ.setName(user.getName() + "的店");
            organ.setSpell(user.getSpell());
            organ.setKindCode("10"); // 店铺
            organ.setParent(root);

            organDao.save(organ);
        }
        else {
            organ = root;
        }

        // 为其创建主机构
        UserOrgans userOrgans = new UserOrgans();
        userOrgans.setUser(user);
        userOrgans.setOrgan(organ);
        userOrgans.setIsPrimary(true);
        userOrgansDao.save(userOrgans);

        return organ;
    }


    /**
     * 为新注册的用户设置角色
     *
     * @param user
     * @return
     */
    private void setRole(User user) {
        String templateRoleId = TEMPLATE_MEMBER_ROLE_ID;
        if ("10".equals(user.getKindCode())) {
            templateRoleId = TEMPLATE_SHOP_ROLE_ID;
        }

        Role tpl = roleDao.get(templateRoleId);
        if (tpl == null) {
            throw new RuntimeException("系统没有初始化相应角色");
        }

        UserRoles userRoles = new UserRoles();
        userRoles.setUser(user);
        userRoles.setRole(tpl);

        userRoleDao.save(userRoles);
    }
}
